package HLLib.base;

import HLCode.HLClass;
import HLCode.HLClassManager;
import HLCode.HLClassManager_H;
import HLCode.HLLibObject;
import HLCode.HLObject;

public class HLList extends HLLibObject {
    public HLClass GetClass(HLClassManager classManager) {
        return classManager.GetLibClass(HLClassManager_H.CLASS_GROUP_TYPE_LIB_BASE, HLClassManager_H.LIB_CLASS_BASE_List);
    }

    public Object[] items;

    public HLList() {
        items = new Object[0];
    }

    public HLList(Object[] items) {
        this.items = items;
    }

    public HLList(int count) {
        items = new Object[count];
    }

    public void SetCount(int count) {
        if (items.length < count) {
            Resize(items.length, count - items.length);
        } else if (items.length > count) {
            Resize(count, count - items.length);
        }
    }

    public void Add(HLObject item) {
        Resize(items.length, 1);
        items[items.length - 1] = item;
    }

    public void Add1(Object item) {
        Resize(items.length, 1);
        items[items.length - 1] = item;
    }

    public void AddRange(HLList list) {
        InsertRange(items.length, list);
    }

    public void Clear() {
        items = new Object[0];
    }

    public int Count() {
        return items.length;
    }

    public HLObject GetItem(int index) {
        return (HLObject) items[index];
    }

    public Object GetItem1(int index) {
        return items[index];
    }

    public void SetItem(int index, HLObject item) {
        items[index] = item;
    }

    public void SetItem1(int index, Object item) {
        items[index] = item;
    }

    public void Insert(int index, HLObject item) {
        Resize(index, 1);
        items[index] = item;
    }

    public void Insert1(int index, Object item) {
        Resize(index, 1);
        items[index] = item;
    }

    public void InsertRange(int index, HLList list) {
        if (list.Count() > 0) {
            Resize(index, list.items.length);
            System.arraycopy(list.items, 0, items, index, list.items.length);
        }
    }

    public int Find(HLObject item) {
        for (int i = 0; i < items.length; i++) {
            if (items[i] == item) {
                return i;
            }
        }

        return -1;
    }

    public int Find1(Object item) {
        for (int i = 0; i < items.length; i++) {
            if (items[i] == item) {
                return i;
            }
        }

        return -1;
    }

    public void CopyAll(int toIndex, HLList list) {
        System.arraycopy(list.items, 0, items, toIndex, list.items.length);
    }

    public void Copy(int toIndex, HLList list, int start, int length) {
        System.arraycopy(list.items, start, items, toIndex, length);
    }

    public void Remove(HLObject object) {
        int index = Find(object);
        if (index >= 0) {
            Resize(index, -1);
        }
    }

    public void Remove1(Object object) {
        int index = Find1(object);
        if (index >= 0) {
            Resize(index, -1);
        }
    }

    public void RemoveAt(int index) {
        Resize(index, -1);
    }

    public void Swap(int indexA, int indexB) {
        Object tmp = items[indexA];
        items[indexA] = items[indexB];
        items[indexB] = tmp;
    }

    public void Resize(int index, int count) {
        items = Resize(items, index, count);
    }

    public static Object[] Resize(Object[] items, int index, int count) {
        int len = items.length;
        Object[] newItems = new Object[len + count];
        System.arraycopy(items, 0, newItems, 0, index);
        if (count > 0) {
            System.arraycopy(items, index, newItems, index + count, len - index);
        } else {
            System.arraycopy(items, index - count, newItems, index, len + count - index);
        }

        return newItems;
    }
}
